package com.suke.czx.modules.wx.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.LoggerFactory;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;

public class AmrToMp3 {
	
	private static org.slf4j.Logger logger = LoggerFactory.getLogger(AmrToMp3.class);
	
	public static void main(String[] args) throws Exception {
		File source = new File("D:\\202419.amr"); // 源文件
		File target = new File("D:\\202419.mp3"); // 目标文件
		changeTemp(source, target);
	}

	public static boolean changeProcess(String sourcePath, String targetPath) {
		try {
			List<String> command = new ArrayList<String>();
			command.add("/root/ffmpeg");
			command.add("-i");
			command.add(sourcePath);
			command.add(targetPath);
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(command);
			Process process = builder.start();
			StringBuffer sbf = new StringBuffer();
			String line = null;
			BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
			while ((line = br.readLine()) != null) {
				sbf.append(line);
				sbf.append("\r\n");
			}
			 String resultInfo = sbf.toString();
			 logger.info("开始文件转换：" + sourcePath);
			 logger.info(resultInfo);
			 logger.info("文件转换结果：" + targetPath);
			 
			 if(new File(targetPath).exists()){
				 return true;
			 }
			 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

	public static void changeTemp(File source, File target) {

		AudioAttributes audio = new AudioAttributes();
		audio.setCodec("libmp3lame");
		EncodingAttributes attrs = new EncodingAttributes();
		attrs.setFormat("mp3");
		attrs.setAudioAttributes(audio);
		Encoder encoder = new Encoder();
		try {
			encoder.encode(source, target, attrs);
		} catch (IllegalArgumentException | EncoderException e) {
			e.printStackTrace();
		}
	}
}
